package com.huibei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huibei.pojo.User;
import com.huibei.service.IUserService;
import com.huibei.util.ReturnData;
import com.huibei.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private IUserService userservice;


    @RequestMapping("getAllUser")
    public Object getUser(){
        System.out.println(userservice.list());
        return null;
    }

    @RequestMapping(value = "/loginServlet",method = RequestMethod.POST)
    public Object loginServlet(@RequestParam Map<String, Object> map){

            QueryWrapper<User> wrapper = new QueryWrapper<>();
            System.out.println("前端传来的用户名和密码为：" + map.get("userName") + "," + map.get("userPassWord"));
            wrapper.eq("USER_NAME", map.get("userName"));
            wrapper.eq("USER_PASSWORD", map.get("userPassWord"));
            List<Map<String,Object>> list = userservice.listMaps(wrapper);
            System.out.println(list);
        if (list.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS, list, "后端交互正常！");
        }else {
            return new ReturnData(StatusCode.USER_NOT_EXIST,map,"数据交互异常");
        }
    }

    @RequestMapping(value = "/registerServlet",method = RequestMethod.POST)
    public Object registerServlet(@RequestParam Map<String, Object> map){
        System.out.println("注册用户的信息为：" + map);
        System.out.println("注册用户的信息为：" + map);
        User user = new User(userservice.getMaxUserId() + 1, map.get("userName") + "",map.get("userPassWord") + "",
              map.get("userPhone") + "" );
       boolean i=userservice.save(user);
        if (i){
            return new ReturnData(StatusCode.REQUEST_SUCCESS, map, "后端交互正常！");
        }else {
            return new ReturnData(StatusCode.RETURN_ERROR,map,"数据交互异常");
        }

    }

}
